package org.jpass.dc.business.conpiler;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.jpass.dc.common.exception.IDEBuildException;
/**
 * 编译项目的类
 * @author Administrator
 *
 */
public class CompileProject {
	
	private Log log = LogFactory.getLog(CompileProject.class);
	
	/** 
     * 执行build.xml文件 
     * @param projectPath  工程路径
     * @param level  日志输出级别(Project.MSG_INFO) 
     * */  
    public void exeBuild(String appPath, String projectPath, int level) throws BuildException { 
    	
		File buildFile = new File(appPath + "/WEB-INF/classes/build.xml");  
        Project p = new Project();
        p.setBaseDir(new File(projectPath));
        //添加日志输出
        PrintStream console_out = System.out;
        PrintStream console_err = System.err;
		
        DefaultLogger consoleLogger = new DefaultLogger();
        String userWorkPath = projectPath.substring(0, projectPath.lastIndexOf("/"));
        this.redirectOutPut(userWorkPath, consoleLogger); // 重定向输出
        //输出信息级别  
        consoleLogger.setMessageOutputLevel(level);  
        p.addBuildListener(consoleLogger);  
        try {
            p.fireBuildStarted(); 
            p.init();
            ProjectHelper helper = ProjectHelper.getProjectHelper();  
            helper.parse(p, buildFile); 
            p.executeTarget(p.getDefaultTarget());  
    	} catch (BuildException e) {
    		e.printStackTrace();
    		throw new IDEBuildException(e.getMessage());
    	} finally {
    		p.fireBuildFinished(null);
    		System.setOut(console_out);
    		System.setOut(console_err);
    	}
        
    }
    
    public void runJavaProject(String appPath, String projectPath, String mainClass, int level) throws BuildException {
    	File buildFile = new File(appPath + "/WEB-INF/classes/build.xml");  
        Project p = new Project();
        p.setBaseDir(new File(projectPath));
        //添加日志输出
        PrintStream console_out = System.out;
        PrintStream console_err = System.err;
		
        DefaultLogger consoleLogger = new DefaultLogger();
        String userWorkPath = projectPath.substring(0, projectPath.lastIndexOf("/"));
        this.redirectOutPut(userWorkPath, consoleLogger); // 重定向输出
        //输出信息级别  
        consoleLogger.setMessageOutputLevel(level);  
        p.addBuildListener(consoleLogger);   
        try {  
            p.fireBuildStarted();  
            p.init();  
            ProjectHelper helper = ProjectHelper.getProjectHelper();  
            helper.parse(p, buildFile);
            p.setProperty("main", mainClass);
            p.executeTarget("run");        
            p.fireBuildFinished(null);  
        } catch (BuildException e) {  
        	e.printStackTrace();
    		throw new IDEBuildException(e.getMessage());
        } finally {
    		p.fireBuildFinished(null);
    		System.setOut(console_out);
    		System.setOut(console_err);
    	}
    }
    
    /**
     * 重定向输出
     * @param userWorkPath
     * @param logger
     */
    private void redirectOutPut(String userWorkPath, DefaultLogger logger) {
    	try {
			PrintStream out = new PrintStream(
					 	new BufferedOutputStream(new FileOutputStream(userWorkPath + "/out.log")));
			PrintStream error = new PrintStream(
					 	new BufferedOutputStream(new FileOutputStream(userWorkPath + "/error.log")));
			System.setOut(out);
	        System.setErr(error);
	        logger.setErrorPrintStream(error);
	        logger.setOutputPrintStream(out);
		} catch (FileNotFoundException e) {
			log.error("找不到输出文件", e);
		}
    }
    
}
